<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" inline label-width="80px">
      <el-form-item label="标题">
        <el-input v-model="queryParams.title" placeholder="请输入" size="small" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="级别">
        <el-input v-model="queryParams.level" placeholder="市级/省级" size="small" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb8">
      <el-button type="primary" size="mini" @click="handleAdd">新增</el-button>
      <el-button type="danger" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
    </div>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" />
      <el-table-column label="标题" prop="title" />
      <el-table-column label="级别" prop="level" width="120" />
      <el-table-column label="授予单位" prop="awardOrg" />
      <el-table-column label="授予日期" prop="awardDate" width="120" />
      <el-table-column label="获奖人/团队" prop="ownerName" />
      <el-table-column label="状态" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==='0'?'success':'info'">{{ scope.row.status==='0'?'在册':'撤销' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" @click="view(scope.row)">查看</el-button>
          <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="700px">
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item label="标题"><el-input v-model="form.title" /></el-form-item>
        <el-form-item label="级别"><el-input v-model="form.level" /></el-form-item>
        <el-form-item label="授予单位"><el-input v-model="form.awardOrg" /></el-form-item>
        <el-form-item label="授予日期"><el-date-picker v-model="form.awardDate" type="date" placeholder="选择日期" /></el-form-item>
        <el-form-item label="获奖人/团队"><el-input v-model="form.ownerName" /></el-form-item>
        <el-form-item label="状态"><el-switch v-model="form.status" active-value="0" inactive-value="1" /></el-form-item>
        <el-form-item label="附件URL"><el-input v-model="form.attachments" placeholder="逗号分隔多个地址" /></el-form-item>
        <el-form-item label="描述"><el-input type="textarea" :rows="6" v-model="form.description" /></el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="open=false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="荣誉详情" :visible.sync="viewOpen" width="700px">
      <h3>{{ form.title }}</h3>
      <p>级别：{{ form.level }} | 授予单位：{{ form.awardOrg }} | 授予日期：{{ form.awardDate }}</p>
      <p>获奖人/团队：{{ form.ownerName }}</p>
      <div v-html="formatDesc(form.description)"></div>
      <div slot="footer">
        <el-button type="primary" @click="viewOpen=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHonor, getHonor, addHonor, updateHonor, delHonor } from '@/api/cms/honor'

export default {
  name: 'HonorAward',
  data() {
    return {
      loading: false,
      list: [], total: 0, ids: [], single: true, multiple: true,
      title: '', open: false, viewOpen: false,
      queryParams: { pageNum: 1, pageSize: 10, title: undefined, level: undefined },
      form: { honorId: undefined, title: '', level: '', awardOrg: '', awardDate: '', ownerName: '', description: '', attachments: '', status: '0' }
    }
  },
  created() { this.getList() },
  methods: {
    getList() {
      this.loading = true
      listHonor(this.queryParams).then(res => { this.list = res.rows; this.total = res.total; this.loading = false })
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleSelectionChange(val) { this.ids = val.map(i=>i.honorId); this.single = val.length!==1; this.multiple = !val.length },
    handleAdd() { this.title='新增荣誉'; this.form = { title:'', level:'', awardOrg:'', awardDate:'', ownerName:'', description:'', attachments:'', status:'0' }; this.open=true },
    handleUpdate(row) { getHonor(row.honorId).then(res=>{ this.form = Object.assign({}, res.data); this.title='编辑荣誉'; this.open=true }) },
    handleDelete(row) { const ids = row.honorId ? row.honorId : this.ids; this.$modal.confirm('确认删除选中数据吗？').then(()=> delHonor(ids)).then(()=>{ this.getList(); this.$modal.msgSuccess('删除成功') }).catch(()=>{}) },
    submitForm() { const req = this.form.honorId ? updateHonor(this.form) : addHonor(this.form); req.then(()=>{ this.$modal.msgSuccess('操作成功'); this.open=false; this.getList() }) },
    view(row) { getHonor(row.honorId).then(res=>{ this.form = res.data; this.viewOpen=true }) },
    formatDesc(text) { return (text || '').replace(/\n/g,'<br/>') }
  }
}
</script>

<style scoped>
.mb8{ margin-bottom: 8px; }
</style>


